<template>
  <div :class="['message', { 'user-message': isUser, 'ai-message': !isUser }]">
    <div class="message-avatar">
      <div class="avatar">
        {{ isUser ? '👤' : '🤖' }}
      </div>
    </div>
    <div class="message-content">
      <div class="message-bubble">
        <div class="message-text" v-html="formattedContent"></div>
        <div class="message-time">{{ formatTime(timestamp) }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChatMessage',
  props: {
    content: {
      type: String,
      required: true
    },
    isUser: {
      type: Boolean,
      default: false
    },
    timestamp: {
      type: Date,
      default: () => new Date()
    }
  },
  computed: {
    formattedContent() {
      // 简单的文本格式化，将换行符转换为 <br>
      return this.content.replace(/\n/g, '<br>')
    }
  },
  methods: {
    formatTime(date) {
      return date.toLocaleTimeString('zh-CN', { 
        hour: '2-digit', 
        minute: '2-digit' 
      })
    }
  }
}
</script>

<style scoped>
.message {
  display: flex;
  margin-bottom: 20px;
  animation: fadeIn 0.3s ease-in;
}

.user-message {
  flex-direction: row-reverse;
}

.message-avatar {
  flex-shrink: 0;
  margin: 0 12px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
}

.user-message .avatar {
  background: linear-gradient(45deg, #764ba2, #667eea);
}

.message-content {
  flex: 1;
  max-width: 70%;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: 18px;
  position: relative;
}

.ai-message .message-bubble {
  background: #f8f9fa;
  border-bottom-left-radius: 6px;
}

.user-message .message-bubble {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border-bottom-right-radius: 6px;
}

.message-text {
  line-height: 1.5;
  word-wrap: break-word;
}

.message-time {
  font-size: 0.75rem;
  opacity: 0.7;
  margin-top: 4px;
  text-align: right;
}

.ai-message .message-time {
  text-align: left;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .message-content {
    max-width: 85%;
  }
  
  .avatar {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }
}
</style>